/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;


import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import pojo.CuaHang;
import pojo.NguoiDung;
import util.HibernateUtil;

/**
 *
 * @author ANNM
 */
public class CuaHangDAO2 {
    public static List<CuaHang> LayCuaHang(){
        List<CuaHang> ds=null;
        Session session=HibernateUtil.getSessionFactory().openSession();
        String hql="select ch from CuaHang ch ";
        Query query=session.createQuery(hql);
        ds=query.list();
        session.close();
        return ds;
    }
    public static CuaHang LayTTCuaHang(int maCuaHang){
        CuaHang ch=null;
        Session session=HibernateUtil.getSessionFactory().openSession();
        try {
            ch = (CuaHang) session.get(CuaHang.class,maCuaHang );
        } catch (HibernateException ex) {
            //log the exception
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ch;
    }
    public static CuaHang LayTTCuaHang(NguoiDung nd){
        CuaHang ch=null;
        Session session=HibernateUtil.getSessionFactory().openSession();
        String tinhtrang="true";        
        String hql="select ch from CuaHang ch,NguoiDung nd where ch.tinhtrang="+tinhtrang+"and nd.cuaHang.maCuaHang=ch.maCuaHang and nd.maNguoiDung="+nd.getTaiKhoan();
        Query query=session.createQuery(hql);
        ch=(CuaHang)query.uniqueResult();
        session.close();
        return ch;
    }
   
    
}
